<style type="text/css">
body,td,th {
	font-size: 14px;
}

body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}

table {
	border-collapse: collapse;
}

caption {
	border-top-width: 1px;
	border-bottom-width: 1px;
	border-top-style: dashed;
	border-bottom-style: dashed;
	border-top-color: #666;
	border-bottom-color: #666;
	font-size: xx-large;
	color: #60C;
	line-height: 50px;
	margin-bottom: 15px;
}

a:link {
	color: #603;
	text-decoration: none;
}

a:visited {
	text-decoration: none;
	color: #603;
}

a:hover {
	text-decoration: none;
	color: #F00;
}

a:active {
	text-decoration: none;
	color: #F00;
}

#back {
	float: right;
}
</style>
<?php
// config variable
$p = basename ( $_SERVER ['PHP_SELF'] );
$pb = substr ( $p, 0, - 11 );
$op = $pb . '.php';
/*
// variable
$aDiff = $conf[$pb]['aD'];
$aUnit = $conf[$pb]['aU'];
*/
$subfile="sub_{$p}";
if (file_exists($subfile)) {include $subfile;exit;}
require '~cache.php';
require 'conf.php';
echo GetHead ();

if (! isset ( $conf [$pb] ) or empty ( $conf [$pb] ) or ! is_array ( $conf [$pb] ))
	die ( 'Please edit the <i>config file</i> first' );
extract ( $conf [$pb], EXTR_PREFIX_SAME, 'conf' );
$sqlTable = empty ( $conf [$pb] ['sT'] ) ? $pb : $conf [$pb] ['sT'];
$sqlPre = empty ( $conf [$pb] ['sP'] ) ? $sqlTable : $conf [$pb] ['sP'];
$sqlId = $sqlPre . '_id';
foreach ( $aDiff as $v )
	$t [] = $sqlPre . '_' . $v;
isset ( $t ) && $aDiff = $t;

// catch get params
$act = $_GET ['act'];
$id = $_GET ['id'];

// handle post data
$aDiff [] = $sqlId;
$aDiff [] = 'submit';
$aDiff = array_flip ( $aDiff );
if (isset ( $_SERVER ['HTTP_REFERER'] ) && ! empty ( $_POST )) {
	$post = array_diff_key ( $_POST, $aDiff );
	// handle upload file
	if ((($_FILES [$pb.'_image'] ["type"] == "image/jpeg") || ($_FILES [$pb.'_image'] ["type"] == "image/pjpeg")) && ($_FILES [$pb.'_image'] ["size"] >0) && ($_FILES [$pb.'_image'] ["error"] == 0)) {
		$filename="../image/" .md5($pb.$post[$pb.'_name']).'.jpg';
		move_uploaded_file ( $_FILES [$pb.'_image'] ["tmp_name"],  $filename );
		$post[$pb.'_image']=$filename;
	} 
	//
	switch ($act) {
		case 'c' :
			$k = implode ( ', ', array_keys ( $post ) );
			$v = implode ( "', '", array_values ( $post ) );
			$sql = "insert into $sqlTable ( $k ) values ( '$v' )";
			if (($t = alter ( $sql )) != false) {
				$msg = GetDiv ( 'msg', '信息添加成功！' . GetLink ( $op, '返回列表' ) );
				$id = $t;
			}
			break;
		case 'e' :
			foreach ( $post as $k => $v ) {
				$value [] = "$k='$v'";
			}
			$v = implode ( ', ', $value );
			$sql = 'update ' . $sqlTable . ' set ' . $v . ' where ' . $sqlId . '=' . $_POST [$sqlId] . ' limit 1';
			if (alter ( $sql )) {
				$msg = GetDiv ( 'msg', '信息修改成功！' . GetLink ( $op, '返回列表' ) );
				$id = $_POST [$sqlId];
			}
			break;
		default :
			exit ( '<script>alert(\'错误，点击返回\');history.back();</script>' );
	}
	
	if (! isset ( $msg )) {
		$msg = GetDiv ( 'msg', '操作失败，请确认信息无误稍后尝试！' );
		$info = $_POST;
	} else {
		$act = 'v';
	}
}

// view
if ($act == 'v' && ! empty ( $id )) {
	
	if (! isset ( $info )) {
		$sql = "select * from $sqlTable where $sqlId = $id ";
		$info = choice ( $sql );
		$info = FormatListWithViewLink ( $info, $aLink, $pb );
		$info = $info [0];
	}
	$t = GetFieldInfo ( $sqlTable, array_diff_key ( $info, $aDiff ) );
	$info = FormatArrayToUI ( $t, $aUnit, $info );
	isset ( $msg ) ? ShowViewAsTableB ( $info, $sqlTable, $msg ) : ShowViewAsTableA ( $info, $sqlTable, GetDiv ( 'back', GetLink ( 'javascript:history.back();', '返回列表' ) ) );

} elseif ($act == 'e' && ! empty ( $id )) {
	
	if (! isset ( $info )) {
		$sql = "select * from $sqlTable where $sqlId = $id ";
		$info = choice ( $sql );
		$info = $info [0];
	}
	$t = GetFieldInfo ( $sqlTable, array_diff_key ( $info, $aDiff ) );
	$info = FormatArrayToUI ( $t, $aUnit, $info, $aLink );
	isset ( $msg ) ? ShowInputAsTableB ( $info, $sqlTable, $msg, '', GetHidden ( array ($sqlId => $id ) ) ) : ShowInputAsTableA ( $info, $sqlTable, '', GetHidden ( array ($sqlId => $id ) ) );

} elseif ($act == 'c') {
	
	$t = array_diff_key ( GetFieldsInfoByTable ( $sqlTable ), $aDiff );
	$info = FormatArrayToUI ( $t, $aUnit, $info, $aLink );
	isset ( $msg ) ? ShowInputAsTableB ( $info, $sqlTable, $msg ) : ShowInputAsTableA ( $info, $sqlTable );

} elseif ($act == 'd') {
	
	$sql = "delete from $sqlTable where $sqlId = $id limit 1";
	if (alter ( $sql )) {
		echo GetDiv ( 'delete_sucess', '删除成功，' . GetLink ( $op, '返回列表' ) );
	} else {
		echo GetDiv ( 'delete_sucess', '删除失败，' . GetLink ( 'history.back();', '返回列表' ) );
	}

} else {
	exit ( '<script>alert(\'错误，点击返回\');history.back();</script>' );
}

echo GetFoot ();
